<?php
namespace App\Http\Middleware;

use App\Exceptions\StandardException;
use App\Modules\User\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class CheckManager
{
    public function handle(Request $request, \Closure $next)
    {
        $user = Auth::user();
        if ($user->level != 3) {
            throw new StandardException('您没有权限访问');
        }

        if (in_array($user->employee_identity, [User::EMPLOYEE_IDENTITY_MIDDEL, User::EMPLOYEE_IDENTITY_PRIMARY])) {
            $shopId = $request->input('shop_id');
            if (empty($shopId)) {
                $request->merge(['shop_id' => $user->shop_id]);
            } elseif (!empty($tmp = array_diff($shopId, $user->shop_id))) {
                throw new StandardException('您没有权限访问该商铺');
            }
        }
        return $next($request);
    }
}
